<?php

namespace app\admin\controller;

use app\common\model\MenuModel;
use think\Controller;
use think\Db;

class Menu extends Intercept
{
    public function index()
    {
        try {
            if (request()->isAjax()) {
                $list = (new MenuModel())->getList();
                $data = [];
                foreach ($list as $v) {
                    $data[] = [
                        'authorityId' => $v['id'],
                        'authorityName' => $v['name'],
                        'menuIcon' => !empty($v['icon']) ? $v['icon'] : '',
                        'parentId' => !empty($v['parent']) ? $v['parent'] : ''
                    ];
                }
                return ["code" => 0, "msg" => '', "count" => 19, "data" => $data];
            }
            return $this->fetch();
        } catch (\Exception $e) {
            echo $e->getMessage();
        }
    }

    public function updateData()
    {
        try {
            if (request()->isPost()) {
                $params = input('param.');
                $mModel = new MenuModel();
                $returnData = $mModel->updateData($params);
                return $returnData;
            }
        } catch (\Exception $e) {
            echo $e->getMessage();
        }
    }

    public function add()
    {
        try {
            $list = Db::name('cms_blog_menu')->where('parent', 0)->select();
            $this->assign('list', $list);
            return $this->fetch('add');
        } catch (\Exception $e) {
            echo $e->getMessage();
        }
    }

    public function edit($id)
    {
        try {
            $list = Db::name('cms_blog_menu')->where('id', $id)->select();
            $res = Db::name('cms_blog_menu')->select();
            $data = [];
            foreach ($res as $arr) {
                $data[$arr['parent']][] = $arr;
            }
            $this->assign('data', $data);
            $this->assign('list', $list);
            return $this->fetch('edit');

        } catch (\Exception $e) {
            echo $e->getMessage();
        }
    }

    public function delete($id)
    {
        try {
            $mModel = new MenuModel();
            $returnData = $mModel->del($id);
            return $returnData;
        } catch (\Exception $e) {
            echo $e->getMessage();
        }
    }

}
